<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never" v-loading="loading">
      <template #header>
        <div class="card-header">
          <h3>药品详情</h3>
          <div class="right-button">
            <el-button @click="goBack">返回</el-button>
            <el-button type="primary" @click="handleEdit" v-permission="'drug:update'">编辑</el-button>
          </div>
        </div>
      </template>

      <div v-if="drugDetail">
        <!-- 基本信息 -->
        <div class="section-title">基本信息</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="药品编码">{{ drugDetail.drugCode || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="药品名称">{{ drugDetail.name || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="通用名">{{ drugDetail.genericName || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="药品分类">{{ drugDetail.category || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="规格">{{ drugDetail.specification || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="剂型">{{ drugDetail.dosageForm || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="drugDetail.status === '启用' ? 'success' : 'danger'">
              {{ drugDetail.status || '未知' }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>

        <!-- 价格与库存 -->
        <div class="section-title">价格与库存</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="售价">¥{{ drugDetail.price || '0.00' }}</el-descriptions-item>
          <el-descriptions-item label="预警数量">{{ drugDetail.alertQuantity || '0' }}</el-descriptions-item>
          <el-descriptions-item label="当前库存">
            <el-tag :type="getInventoryTagType(drugDetail.inventory?.totalQuantity)">
              {{ drugDetail.inventory?.totalQuantity || 0 }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>

        <!-- 厂家信息 -->
        <div class="section-title">厂家信息</div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="生产厂家">{{ drugDetail.manufacturer || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="批准文号">{{ drugDetail.approvalNumber || '未设置' }}</el-descriptions-item>
        </el-descriptions>

        <!-- 处方信息 -->
        <div class="section-title">处方信息</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="是否处方药">
            <el-tag :type="drugDetail.properties?.isPrescription ? 'warning' : 'info'">
              {{ drugDetail.properties?.isPrescription ? '是' : '否' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="是否管制药品">
            <el-tag :type="drugDetail.properties?.isControlled ? 'danger' : 'info'">
              {{ drugDetail.properties?.isControlled ? '是' : '否' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="存储条件">{{ drugDetail.properties?.storageCondition || '未设置' }}</el-descriptions-item>
        </el-descriptions>

        <!-- 用法用量 -->
        <div class="section-title">用法用量</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="用量">{{ drugDetail.usage?.dosage || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="频次">{{ drugDetail.usage?.frequency || '未设置' }}</el-descriptions-item>
          <el-descriptions-item label="用药途径">{{ drugDetail.usage?.route || '未设置' }}</el-descriptions-item>
        </el-descriptions>

        <!-- 适应症和禁忌症 -->
        <div class="section-title">适应症和禁忌症</div>
        <el-descriptions :column="1" border>
          <el-descriptions-item label="适应症">
            <el-tag v-for="(item, index) in (drugDetail.indications || [])" :key="index" class="tag-item">
              {{ item }}
            </el-tag>
            <span v-if="!drugDetail.indications || drugDetail.indications.length === 0">无</span>
          </el-descriptions-item>
          <el-descriptions-item label="禁忌症">
            <el-tag v-for="(item, index) in (drugDetail.contraindications || [])" :key="index" class="tag-item" type="warning">
              {{ item }}
            </el-tag>
            <span v-if="!drugDetail.contraindications || drugDetail.contraindications.length === 0">无</span>
          </el-descriptions-item>
          <el-descriptions-item label="不良反应">
            <el-tag v-for="(item, index) in (drugDetail.sideEffects || [])" :key="index" class="tag-item" type="danger">
              {{ item }}
            </el-tag>
            <span v-if="!drugDetail.sideEffects || drugDetail.sideEffects.length === 0">无</span>
          </el-descriptions-item>
        </el-descriptions>

        <!-- 库存明细 -->
        <div class="section-title">库存明细</div>
        <el-table 
          :data="drugDetail.inventory?.details || []" 
          border 
          stripe
          style="width: 100%"
        >
          <el-table-column prop="batchNumber" label="批号" width="120">
            <template #default="scope">
              {{ scope.row.batchNumber || '未知批号' }}
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="数量" width="100">
            <template #default="scope">
              {{ scope.row.quantity || 0 }}
            </template>
          </el-table-column>
          <el-table-column prop="location" label="库位" width="150">
            <template #default="scope">
              {{ scope.row.location || '未设置' }}
            </template>
          </el-table-column>
          <el-table-column prop="expirationDate" label="有效期" width="120">
            <template #default="scope">
              {{ scope.row.expirationDate || '未设置' }}
            </template>
          </el-table-column>
          <el-table-column prop="manufacturingDate" label="生产日期" width="120">
            <template #default="scope">
              {{ scope.row.manufacturingDate || '未设置' }}
            </template>
          </el-table-column>
          <el-table-column prop="supplier" label="供应商" min-width="150" show-overflow-tooltip>
            <template #default="scope">
              {{ scope.row.supplier || '未设置' }}
            </template>
          </el-table-column>
          <el-table-column prop="receiveDate" label="入库日期" width="120">
            <template #default="scope">
              {{ scope.row.receiveDate || '未设置' }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template #default="scope">
              <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'">
                {{ scope.row.status || '未知' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <div v-else-if="!loading" class="empty-data">
        <el-empty description="未找到药品详情数据" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { drugAPI } from '@/utils/api'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const drugDetail = ref(null)

// 获取药品详情
const fetchDrugDetail = async () => {
  const id = route.params.id
  if (!id) {
    ElMessage.error('药品ID不能为空')
    return
  }
  
  loading.value = true
  
  try {
    console.log('正在获取药品详情, ID:', id)
    const res = await drugAPI.getDrug(id)
    console.log('药品详情API响应:', res)
    
    if (res.success && res.data) {
      drugDetail.value = res.data
      console.log('药品详情数据设置成功:', drugDetail.value)
    } else {
      console.error('药品详情获取失败:', res)
      ElMessage.error('获取药品详情失败: ' + (res.message || '未知错误'))
    }
  } catch (error) {
    console.error('获取药品详情异常:', error)
    ElMessage.error('获取药品详情失败: ' + (error.message || '未知错误'))
  } finally {
    loading.value = false
  }
}

// 获取库存标签类型
const getInventoryTagType = (quantity) => {
  const numQuantity = parseInt(quantity, 10)
  if (isNaN(numQuantity)) return 'info'
  
  const alertQuantity = parseInt(drugDetail.value?.alertQuantity, 10) || 0
  
  if (numQuantity <= 0) return 'danger'
  if (alertQuantity > 0 && numQuantity <= alertQuantity) return 'warning'
  return 'success'
}

// 编辑药品
const handleEdit = () => {
  router.push(`/drugs/edit/${route.params.id}`)
}

// 返回列表页
const goBack = () => {
  router.push('/drugs/list')
}

// 初始化
onMounted(() => {
  fetchDrugDetail()
})
</script>

<style scoped>
.section-title {
  font-size: 16px;
  font-weight: bold;
  margin: 20px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right-button {
  display: flex;
  gap: 10px;
}

.tag-item {
  margin-right: 8px;
  margin-bottom: 8px;
}

.empty-data {
  padding: 30px 0;
}
</style> 